package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.category.CategoryGoodsJson;
import com.drinks.yiniang.entity.category.CategoryHotBrandJson;
import com.drinks.yiniang.entity.category.CategoryJson;
import com.drinks.yiniang.entity.category.SubCategoryJson;
import com.drinks.yiniang.entity.comment.GoodsCommentJson;
import com.drinks.yiniang.entity.comment.MyCommentsJson;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.goods.GoodsDetailJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.goods.HomeDataJson;
import com.drinks.yiniang.entity.goods.HomeGoodsJson;
import com.drinks.yiniang.entity.goods.RecommendGoodsJson;
import com.drinks.yiniang.entity.goods.SearchGoodsJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 商品 api
 * Created by han on 2019/3/6 0006.
 */

public class GoodsApiIO extends BaseApiIO {
    volatile static GoodsApiIO instance;

    static public GoodsApiIO getInstance() {
        if (instance == null) {
            synchronized (GoodsApiIO.class) {
                if (instance == null) {
                    instance = new GoodsApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取首页数据
     *
     * @param callback
     */
    public void getHomeData(final APIRequestCallback<HomeDataJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_GET_HOME_DATA, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取首页数据===onSuccess");

                HomeDataJson mDataJson = JSON.parseObject(res, HomeDataJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 首页商品列表
     *
     * @param attrsId       属性id
     * @param subCategoryId 子分类id
     * @param callback
     */
    public void getHomeGoodsList(String attrsId, String subCategoryId, int page, final APIRequestCallback<HomeGoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("attribute_id", attrsId);
        paramsMap.put("category_id", subCategoryId);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_HOME_GOODS_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("首页商品列表===onSuccess");

                HomeGoodsJson mDataJson = JSON.parseObject(res, HomeGoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取商品分类
     *
     * @param callback
     */
    public void getGoodsCategory(final APIRequestCallback<CategoryJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_CATEGORY_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取商品分类===onSuccess");

                CategoryJson mDataJson = JSON.parseObject(res, CategoryJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getCategories() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 获取分类下子分类
     *
     * @param categoryId 父类id
     * @param callback
     */
    public void getSubCategory(String categoryId, final APIRequestCallback<SubCategoryJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("category_id", categoryId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_SUB_CATEGORY_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取分类下子分类===onSuccess");

                SubCategoryJson mDataJson = JSON.parseObject(res, SubCategoryJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }


    /**
     * 分类商品列表
     *
     * @param categoryId 分类id
     * @param screenType 序方式 0销量 1价格 2新品 3热评
     * @param sortType   排序类型 1升序 2降序
     * @param styleId    -1为你推荐 0全部 >0正常
     * @param attrsId    属性ID
     * @param maxPrice   价格高值
     * @param minPrice   价格低值
     * @param brandId    品牌ID
     * @param originId   原产地ID
     * @param alcoholId  酒精度ID
     * @param capacity   容量
     * @param packType   包装方式 字符串
     * @param page       页码
     * @param callback
     */
    public void getCategoryGoodsList(String categoryId, String screenType, String sortType, String styleId, String attrsId, String maxPrice, String minPrice,
                                     String brandId, String originId, String alcoholId, String capacity, String packType, int page,
                                     final APIRequestCallback<CategoryGoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("category_id", categoryId);
        paramsMap.put("order_type", screenType);
        paramsMap.put("type_sort", sortType);
        paramsMap.put("style_id", styleId);
        paramsMap.put("attribute_id", attrsId);
        paramsMap.put("price_low", minPrice);
        paramsMap.put("price_high", maxPrice);
        paramsMap.put("brand_id", brandId);
        paramsMap.put("origin_id", originId);
        paramsMap.put("alcohol_id", alcoholId);
        paramsMap.put("capacity", capacity);
        paramsMap.put("pack_method", packType);
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_CATEGORY_GOODS_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("分类商品列表===onSuccess");

                CategoryGoodsJson mDataJson = JSON.parseObject(res, CategoryGoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 分类下热门品牌列表
     *
     * @param categoryId 分类id
     * @param callback
     */
    public void getCategoryHotBrandList(String categoryId, final APIRequestCallback<CategoryHotBrandJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("category_id", categoryId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_CATEGORY_HOT_BRAND_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("分类下热门品牌列表===onSuccess");

                CategoryHotBrandJson mDataJson = JSON.parseObject(res, CategoryHotBrandJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 智能推荐的商品列表
     *
     * @param callback
     */
    public void getRecommendGoodsList(final APIRequestCallback<RecommendGoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        //fansId
        paramsMap.put("fans_id", SharedPreUtils.getInstance().getUserID());

        MainApiIO.getInstance().postRequest(HttpUrls.API_RECOMMEND_GOODS_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("智能推荐的商品列表===onSuccess");

                RecommendGoodsJson mDataJson = JSON.parseObject(res, RecommendGoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 智能推荐的商品不再显示
     *
     * @param goodsId  商品id
     * @param callback
     */
    public void doGoodsNotShowAgain(String goodsId, final APIRequestCallback<BaseJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("goods_id", goodsId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_RECOMMEND_GOODS_NOT_SHOW_AGAIN, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("智能推荐的商品不再显示===onSuccess");

                BaseJson mDataJson = JSON.parseObject(res, BaseJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }


    /**
     * 搜索商品
     *
     * @param keyword  关键词
     * @param callback
     */
    public void doGoodsSearch(int page, String keyword, final APIRequestCallback<SearchGoodsJson, Tuple2<Integer, String>> callback) {
        doGoodsSearch(page, null, keyword, callback);
    }

    /**
     * 搜索商品
     *
     * @param categoryId 指定分类
     * @param keyword    关键词
     * @param callback
     */
    public void doGoodsSearch(int page, String categoryId, String keyword, final APIRequestCallback<SearchGoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);
        paramsMap.put("category_id", categoryId);
        paramsMap.put("keyword", keyword);

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_SEARCH, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("搜索商品===onSuccess");

                SearchGoodsJson mDataJson = JSON.parseObject(res, SearchGoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 商品详情
     *
     * @param goodsId  商品id
     * @param callback
     */
    public void getGoodsDetail(String goodsId, final APIRequestCallback<GoodsDetailJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("goods_id", goodsId);
        paramsMap.put("fans_id", SharedPreUtils.getInstance().getUserID());

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_DETAIL, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("商品详情===onSuccess");

                GoodsDetailJson mDataJson = JSON.parseObject(res, GoodsDetailJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 收藏或取消收藏商品
     *
     * @param goodsId  商品id
     * @param callback
     */
    public void doGoodsCollectOrCancel(String goodsId, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        paramsMap.put("goods_id", goodsId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_COLLECT_OR_CANCEL, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("收藏或取消收藏商品===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 商品评论列表
     *
     * @param goodsId  商品id
     * @param type     类型 0全部 1专家
     * @param page     页码
     * @param callback
     */
    public void getGoodsComments(String goodsId, String type, int page, final APIRequestCallback<GoodsCommentJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("goods_id", goodsId);
        paramsMap.put("type", type);
        paramsMap.put("fans_id", SharedPreUtils.getInstance().getUserID());
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_COMMENTS_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("商品评论列表===onSuccess");

                GoodsCommentJson mDataJson = JSON.parseObject(res, GoodsCommentJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 商品评论点赞与取消点赞
     *
     * @param commentId 评论id
     * @param callback
     */
    public void doGoodsCommentsLike(String commentId, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("comment_id", commentId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_GOODS_COMMENT_LIKE, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("商品评论点赞与取消点赞===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 我的点评列表
     *
     * @param callback
     */
    public void getMyGoodsComments(int page, final APIRequestCallback<GoodsJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MY_COMMUNITY_COMMENTS, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("我的点评列表===onSuccess");

                GoodsJson mDataJson = JSON.parseObject(res, GoodsJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null && mDataJson.getData().getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
